<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	
	class Post extends Admin_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('database_model');
		}

		public function index(){
			if(!is_user_logged_in()){
				redirect('admin/login', 'refresh');
			}
			
			$this->load->library('pagination');
			$config['base_url']   = base_url().'admin/post/';
			$config['total_rows'] = $this->database_model->Count('ci_posts', array('post_type' => 'post'));
			$config['per_page']   = 4;
			$config['num_links']  = 5;
			$this->pagination->initialize($config); 

			$rs       = $this->database_model->GetWhere('ci_posts', array('post_type' => 'post'), $config['per_page'], $this->uri->segment(3));
			$total    = $rs->num_rows();
			$post_arr = new stdClass;
			$post     = $rs->result();
			$i=0;
			foreach($post as $item){
				$author_rs = $this->database_model->GetWhere('ci_users', array('ID' => $item->post_author))->row();
				$terms_rs = $this->database_model->GetWhere('ci_relations', array('post_id' => $item->ID))->result();
				$term = '';
				foreach($terms_rs as $term_item){
					$term_item_rs = $this->database_model->GetWhere('ci_terms', array('term_id' => $term_item->term_id))->row();
					if(empty($term)) $term.= $term_item_rs->term_name;
					else $term.=', '.$term_item_rs->term_name;
				}
				$post_arr->$i->post_id     = $item->ID;
				$post_arr->$i->post_title  = $item->post_title;
				$post_arr->$i->post_date   = $item->post_date;
				$post_arr->$i->post_status = $item->post_status;
				$post_arr->$i->categories  = $term;
				$post_arr->$i->post_author = $author_rs->Username;
				$i++;
			}
			$links = $this->pagination->create_links();

			$post_item = getView('modules/post-item',array('data' => $post_arr, 'pagination' =>$links),true);
			$data = array(
				'header'      => 'Posts',
				'menu_active' =>"3.1",
				'content'     => $post_item
				);
			$this->load->view('admin/post', $data);
		}


		public function moveTrash(){
			if(!serverReferer()){
				redirect('admin/post', 'refresh');
			}

			$post_id = $this->input->post('post_id', TRUE);

			if($post_id == FALSE)
				redirect('admin/post', 'refresh');
			
			$post_id = trim($post_id);
			$where = array(
				'ID' => $post_id
				);
			$data = array(
				'post_status' => 'trash'
				);
			$rs = $this->database_model->Update('ci_posts', $data, $where);
			if($rs){
				$json = array(
					'status' => '1',
					'message' => 'Updated successfully'
					);
				echo json_encode($json);
			}else{
				$json = array(
					'status' => '0',
					'message' => 'Update failed'
					);
				echo json_encode($json);
			}
			
		}

		public function movePublish(){
			if(!serverReferer()){
				redirect('admin/post', 'refresh');
			}

			$post_id = $this->input->post('post_id', TRUE);

			if($post_id == FALSE)
				redirect('admin/post', 'refresh');

			$post_id = trim($post_id);
			$where = array(
				'ID' => $post_id
				);
			$data = array(
				'post_status' => 'publish'
				);
			$rs = $this->database_model->Update('ci_posts', $data, $where);
			if($rs){
				$json = array(
					'status' => '1',
					'message' => 'Updated successfully'
					);
				echo json_encode($json);
			}else{
				$json = array(
					'status' => '0',
					'message' => 'Update failed'
					);
				echo json_encode($json);
			}
		}

		public function deletePost(){
			if(!serverReferer()){
				redirect('admin/post', 'refresh');
			}
			
			$post_id = $this->input->post('post_id', TRUE);

			if($post_id == FALSE)
				redirect('admin/post', 'refresh');

			$post_id = trim($post_id);

			$where = array(
				'ID' => $post_id
				);
			$where1 = array(
				'post_id' => $post_id
				);
			try{
				$rs = $this->database_model->Delete('ci_posts', $where);
				$rs1 = $this->database_model->Delete('ci_relations', $where1);
				$json = array(
					'status' => '1',
					'message' => 'Delete successfully'
					);
				echo json_encode($json);
			}catch(Exceptions $e){
				$json = array(
					'status' => '0',
					'message' => 'Delete failed'
					);
				echo json_encode($json);
			}
		}
	}
?>